package com.yupi.springbootinit.RabbitMQ;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class MqProducer {
    private final static String WORK_EXCHANGE_NAME = "work";
    private final static String DEAD_EXCHANGE_NAME = "dead";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection()) {
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(BIMQStringEnum.BI_EXCHANGE, "direct");
            channel.queueDeclare(BIMQStringEnum.BI_QUEUE, true, false, false, null);
            channel.queueBind(BIMQStringEnum.BI_QUEUE, BIMQStringEnum.BI_EXCHANGE, BIMQStringEnum.BI_ROUTING_KEY);

            //durable:持久化  exclusive
            //exclusive ：设置是否排他。true排他的。如果一个队列声明为排他队列，该队列仅对首次声明它的连接可见，并在连接断开时自动删除
//            channel.exchangeDeclare(DEAD_EXCHANGE_NAME, "direct");
//            //绑定队列和交换机
//            channel.queueDeclare("ADead", true, false, false, null);
//            channel.queueBind("ADead", DEAD_EXCHANGE_NAME, "ADead");
//            channel.queueDeclare("BDead", true, false, false, null);
//            channel.queueBind("BDead", DEAD_EXCHANGE_NAME, "BDead");


//            Scanner scanner = new Scanner(System.in);
//            DeliverCallback deadACallback = (consumerTag, deliver) -> {
//                String AResponse = new String(deliver.getBody(), StandardCharsets.UTF_8);
//                System.out.println("[ADead处理人消费了:]" + AResponse);
//            };
//            DeliverCallback deadBCallback = (consumerTag, deliver) -> {
//                String BResponse = new String(deliver.getBody(), StandardCharsets.UTF_8);
//                System.out.println("[BDead处理人消费了:]" + BResponse);
//            };
//            channel.basicConsume("ADead", false, deadACallback, consumerTag -> {
//            });
//            channel.basicConsume("BDead", false, deadBCallback, consumerTag -> {
//            });
//            while (scanner.hasNext()) {
//                String next = scanner.nextLine();
//                String[] s = next.split(" ");
//                String message = s[0];
//                String direct = s[1];
//                channel.basicPublish(WORK_EXCHANGE_NAME, direct, null, message.getBytes(StandardCharsets.UTF_8));
//                System.out.println(" [x] Sent '" + message + "'");
//            }
        }
    }
}
